﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using HodhodNews.Extraction;
using Googler.DAL;
using Googler.BLL;

namespace Googler.Web
{
    public partial class PrintPreviewTest : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        private void ExtractAndView(ExtractionAlgorithm algorithm)
        {
            lblResult.Visible = true;

            var result = NewsExtractionProvider.GetProvider(algorithm).Extract(new ExtracationOptions()
             {
                 NewsUrl = txtLink.Text,
                 NewsSummaryText = txtKeywords.Text,
                 NewsTitle = ""
             });

            if (result != null)
            {
                lblResult.Text = result.Text;
            }

        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            lblResult.Text = "لا يمكن استقطاع المقالة من الوصلة المستهدفة";

            hlExtractedFrom.Text = txtLink.Text;
            hlExtractedFrom.NavigateUrl = txtLink.Text.Trim();

            this.ExtractAndView(ExtractionAlgorithm.CommonParent);
            divFeedback.Visible = true;
        }

        protected void btnFaild_Click(object sender, EventArgs e)
        {
            var context = new DataContext();
            TestedUrl url = context.TestedUrls.Where(u => u.Url == txtLink.Text).FirstOrDefault();
            if (url == null)
            {
                url = new TestedUrl();
                context.AddToTestedUrls(url);
            }

            url.Url = txtLink.Text;
            url.FaildToExtract = true;
            context.SaveChanges();
        }

        protected void btnSuccess_Click(object sender, EventArgs e)
        {
            var context = new DataContext();
            TestedUrl url = context.TestedUrls.Where(u => u.Url == txtLink.Text).FirstOrDefault();
            if (url == null)
            {
                url = new TestedUrl();
                context.AddToTestedUrls(url);
            }
            url.Url = txtLink.Text;
            url.FaildToExtract = true;
            context.SaveChanges();
        }

        protected void btnReextract_Click(object sender, EventArgs e)
        {
            lblResult.Text = "لا يمكن استقطاع المقالة من الوصلة المستهدفة";

            hlExtractedFrom.Text = txtLink.Text;
            hlExtractedFrom.NavigateUrl = txtLink.Text.Trim();

            this.ExtractAndView(ExtractionAlgorithm.Boilerpipe);
            divFeedback.Visible = true;
        }
    }
}